package vin.pth.authentication.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import pth.authentication.config.AuthenticationProperties;
import pth.authentication.service.AuthenticationUserService;
import pth.authentication.service.DefaultRbacServiceServiceImpl;
import pth.authentication.service.RbacService;
import vin.pth.authentication.filter.AuthenticationFilter;
import vin.pth.authentication.handler.AuthenticationHandler;

/**
 * 鉴权Bean注册器.
 *
 * @author cocoon
 */
@EnableConfigurationProperties(AuthenticationProperties.class)
@ComponentScan("vin.pth.authentication.*")
@ConditionalOnProperty(name = "cocoon-security.authentication.enable", matchIfMissing = true)
@Configuration
public class AuthenticationBeanRegister {

  /**
   * 配置文件加载.
   *
   * @return AuthenticationProperties
   */
  @Bean
  public AuthenticationProperties authenticationProperties() {
    return new AuthenticationProperties();
  }

  /**
   * 授权拦截器.
   *
   * @param authenticationUserService authenticationUserService
   * @param rbacService               rbacService
   * @param authenticationHandler     authenticationHandler
   * @return AuthenticationFilter
   */
  @Bean
  public AuthenticationFilter authenticationFilter(
      AuthenticationUserService authenticationUserService,
      RbacService rbacService,
      AuthenticationHandler authenticationHandler) {
    return new AuthenticationFilter(authenticationUserService, rbacService, authenticationHandler);

  }

  /**
   * 默认的继续角色的权限校验.
   *
   * @return RbacService
   */
  @ConditionalOnMissingBean(RbacService.class)
  @Bean
  public RbacService rbacService() {
    return new DefaultRbacServiceServiceImpl();
  }


}
